/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.moravian.managers;

import com.sun.java.swing.plaf.windows.WindowsTreeUI;
import edu.moravian.entity.Enemy;
import edu.moravian.math.Point2D;
import edu.moravian.player.PlayerInfo;
import edu.moravian.sound.SoundManager;
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 *
 * @author ronaghan
 */
public class EnemyManager
{

    private List<Enemy> enemies;
    private List<Enemy> deadEnemies;
    private List<Enemy> reachedGoal;
    private static File explosionSound;

    public EnemyManager()
    {
        enemies = new CopyOnWriteArrayList<Enemy>();
        deadEnemies = new CopyOnWriteArrayList<Enemy>();
        reachedGoal = new CopyOnWriteArrayList<Enemy>();
        initSoundFiles();
    }

    public synchronized void addEnemy(Enemy toAdd)
    {
        enemies.add(toAdd);
    }

    public synchronized void removeEnemy(Enemy toRemove)
    {
        enemies.remove(toRemove);
    }

    public synchronized Iterable<Enemy> getEnemiesInRange(Point2D location, double radius)
    {
        List<Enemy> inRange = new ArrayList<Enemy>();
        for (Enemy enemy : enemies)
        {
            double distSq = enemy.getLocation().distanceSq(location);
            if (distSq < (radius * radius))
            {
                inRange.add(enemy);
            }
        }
        return Collections.unmodifiableList(inRange);
    }

    public synchronized void addDeadEnemy(Enemy enemy)
    {
        deadEnemies.add(enemy);
    }

    public synchronized void update(double delta)
    {

        for (Enemy enemy : enemies)
        {
            if (enemy.getHealth() < 0)
            {
                addDeadEnemy(enemy);
            }
        }

        // remove all the dead enemies, and clear the dead enemy list
        for (Enemy deadEnemy : deadEnemies)
        {
            enemies.remove(deadEnemy);
            PlayerInfo.setMonies(PlayerInfo.getMonies()+20);
            PlayerInfo.setEnemiesKilled(PlayerInfo.getEnemiesKilled()+1);
            
            SoundManager.playFromFile(explosionSound);
        }
        deadEnemies.clear();

        // update the postition of all the enemies
        for (Enemy enemy : enemies)
        {
            enemy.update(delta);
        }

        // remove enemies that reached the goal
        for (Enemy enemy : enemies)
        {
            if (enemy.reachedGoal())
            {
                reachedGoal.add(enemy);
                PlayerInfo.setLivesRemaining(PlayerInfo.getLivesRemaining()-1);
            }
        }

        for (Enemy enemy : reachedGoal)
        {

            enemies.remove(enemy);
        }

    }

    public synchronized Iterable<Enemy> getEnemies()
    {
        return Collections.unmodifiableList(enemies);
    }

    private class CollisionPair
    {

        private Enemy one;
        private Enemy two;

        private CollisionPair(Enemy one, Enemy two)
        {
            this.one = one;
            this.two = two;
        }

        public Enemy getOne()
        {
            return one;
        }

        public Enemy getTwo()
        {
            return two;
        }

    }

    private void initSoundFiles()
    {
        explosionSound = new File("src/main/resources/sounds/splosions/explode1.wav");
    }

}
